﻿if (!hDUnivSim_RootPath)
    var hDUnivSim_RootPath = 'Scripts/hDUniverseSimulator';

var hDUnivSim_JsUtils =
{
    IncludeJsReference: function (jsFilePath) {
        if (hDUniverseSimulator_AreJsAndCssIncluded === true)
            return;

        var useRootPath = true;
        if (arguments[1])
            useRootPath = Boolean(arguments[1]);

        var filePath = useRootPath ? hDUnivSim_RootPath + "/" + jsFilePath : jsFilePath;

        $("head").append("<script src='" + filePath + "' type='text/javascript'></script>");
    }
    , IncludeCssReference: function (cssFilePath) {
        if (hDUniverseSimulator_AreJsAndCssIncluded === true)
            return;

        var useRootPath = true;
        if (arguments[1])
            useRootPath = Boolean(arguments[1]);

        var filePath = useRootPath ? hDUnivSim_RootPath + "/" + cssFilePath : cssFilePath;

        $("head").append("<link href='" + filePath + "' type='text/css' rel='Stylesheet' />");
    }
};

//References BEGIN
hDUnivSim_JsUtils.IncludeCssReference("css/styles.css");

hDUnivSim_JsUtils.IncludeJsReference("Common/Align.js");
hDUnivSim_JsUtils.IncludeJsReference("Common/Point.js");
hDUnivSim_JsUtils.IncludeJsReference("Common/Size.js");
hDUnivSim_JsUtils.IncludeJsReference("Common/Rectangle.js");
hDUnivSim_JsUtils.IncludeJsReference("Common/Range.js");
hDUnivSim_JsUtils.IncludeJsReference("Common/Vector.js");

hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/UniverseEntity.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/Planet.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/Star.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/SolarSystem.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/Galaxy.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/StarField.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/ShootingStar.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseBL/Universe.js");

hDUnivSim_JsUtils.IncludeJsReference("Physics/BasicPhysics.js");

hDUnivSim_JsUtils.IncludeJsReference("Drawing/Animation/AnimationBase.js");
hDUnivSim_JsUtils.IncludeJsReference("Drawing/Animation/MetroStyleAnimation.js");

hDUnivSim_JsUtils.IncludeJsReference("Drawing/UniverseEntityDrawBase.js");
hDUnivSim_JsUtils.IncludeJsReference("Drawing/InPageHtmlFixtureDrawer.js");
hDUnivSim_JsUtils.IncludeJsReference("Drawing/MetroStyleDrawer.js");
hDUnivSim_JsUtils.IncludeJsReference("Drawing/DrawingUtility.js");

hDUnivSim_JsUtils.IncludeJsReference("UniverseFixture/BaseFixture.js");
hDUnivSim_JsUtils.IncludeJsReference("UniverseFixture/InPageHtmlFixture.js");
//References END

function hDUnivSim(holderJqSelector, universeDrawer, universeFixtureProvider) {

    //Privates
    var hDUnivSim = this;

    var holderJq = $(holderJqSelector);
    var canvasJq = null;
    if (universeDrawer == null)
        universeDrawer = new UniverseEntityDrawBase();
    if (universeFixtureProvider == null)
        universeFixtureProvider = BaseFixture;
    DrawingUtility.UniverseDrawer = universeDrawer;
    this.UniverseFixture = universeFixtureProvider.GetFixture();
    this.AlignVertical = Align.Vertical.Middle;
    this.AlignHorizontal = Align.Horizontal.Center;
    this.IsZoomEnabled = true;

    //Public methods
    this.InitHtml = function () {
        DrawingUtility.DrawToHtml(this.UniverseFixture, canvasJq);
        $(window).resize();
    };
    
    //Constructor BEGIN

    canvasJq = $('<div class="hDUnivSim_Canvas" style="width:' + holderJq.outerWidth() + 'px; height:' + holderJq.outerHeight() + 'px; position: relative;"></div>');

    holderJq.append(canvasJq);
    $(window).resize(function () {
        holderWidth = holderJq.outerWidth();
        holderHeight = holderJq.outerHeight();

        var universeJq = canvasJq.children(".Universe");

        canvasJq.css("width", holderWidth);
        canvasJq.css("height", holderHeight);

        switch (hDUnivSim.AlignHorizontal) {
            case Align.Horizontal.Left:
                universeJq.css('left', "0px");
                break;
            case Align.Horizontal.Right:
                universeJq.css('left', (holderWidth - universeJq.outerWidth()) + "px");
                break;
            case Align.Horizontal.Center:
            default:
                universeJq.css('left', (holderWidth - universeJq.outerWidth()) / 2 + "px");
                break;
        };

        switch (hDUnivSim.AlignVertical) {
            case Align.Vertical.Top:
                universeJq.css('top', "0px");
                break;
            case Align.Vertical.Bottom:
                universeJq.css('top', (holderHeight - universeJq.outerHeight()) + "px");
                break;
            case Align.Vertical.Middle:
            default:
                universeJq.css('top', (holderHeight - universeJq.outerHeight()) / 2 + "px");
                break;
        };
    });

    var universeFixture = this.UniverseFixture;
    $(window).keypress(function (e) {
        if (!this.IsZoomEnabled)
            return;
        var step = 0.05;
        var currentViewDepth = universeFixture.ViewDepth();
        if (e.charCode == 43) //PLUS
            universeFixture.ViewDepth(currentViewDepth - step);
        else if (e.charCode == 45) //MINUS
            universeFixture.ViewDepth(currentViewDepth + step);
        else if (e.charCode == 42) //ASTERISK
            universeFixture.ViewDepth(0);
    });

    //Constructor END

};